package com.example.demo.controller;

import com.example.demo.annotation.Roles;
import com.example.demo.dto.PageDTO;
import com.example.demo.dto.UserAddDTO;
import com.example.demo.dto.UserLoginDTO;
import com.example.demo.dto.UserUpdateDTO;
import com.example.demo.pojo.User;
import com.example.demo.pojo.Result;
import com.example.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "员工接口")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @PostMapping
    @Operation(summary = "新增员工")
    public Result addUser(@RequestBody UserAddDTO userDTO) {
        log.info("addUser {}", userDTO);
        userService.addUser(userDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询员工")
    public Result<User> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @PostMapping("/login")
    @Operation(summary = "登陆")
    public Result<String> loginUser(@RequestBody UserLoginDTO userLoginDTO) {
        String jwt = userService.loginUser(userLoginDTO);
        return Result.success(jwt);
    }

    @GetMapping("/bc")
    @Operation(summary = "查看员工bc")
    @Roles(values = "sa")
    public Result<List<User>> getUserBC(PageDTO pageDTO) {
        return Result.success(userService.getUserBC(pageDTO));
    }

    @GetMapping("/c")
    @Operation(summary = "查看员工c")
    @Roles(values = "da")
    public Result<List<User>> getUserC(PageDTO pageDTO) {
        return Result.success(userService.getUserC(pageDTO));
    }

    @PostMapping("/roles")
    @Operation(summary = "修改员工权限")
    @Roles(values = "sa")
    public Result updateUser(@RequestBody UserUpdateDTO userDTO) {
        userService.updateUser(userDTO);
        return Result.success();
    }
}
